package gms.simulation;

import java.util.Random;
import java.io.Writer;

/**
 * Created by IntelliJ IDEA.
 * User: Anton Dunaev
 * Date: 07.09.2007
 * Time: 16:49:01
 */
public class Send extends Operation
{
    gms.cwf.Send op;
    NetSimulation net_sim = null;
    boolean is_add_to_time_line = false;

    public Send( gms.cwf.Send op)
    {
        this.op = op;
        net_sim = NetSimulation.getInstance();
    }

    public long getSrc_node_id()
    {
        return op.getSrc_node_id();
    }

    public long getDest_node_id()
    {
        return op.getDest_node_id();
    }

    public double getAmount()
    {
        return op.getAmount();
    }

    public boolean tryStep()
            throws Exception
    {
        if( !is_add_to_time_line )
        {
            net_sim.addAction( this );
            is_add_to_time_line = true;
            return true;
//        }else
//        if( !isApproved() & net_sim.isApproved( this ) )
//        {
//            return true;
        }else
            return false;
    }

    public void printStatus(Writer w, String prefix)
    {
        try
        {
            Long [] line_ids = NetSimulation.getSmap().getPath( op.getSrc_node_id(), op.getDest_node_id());
            w.write( prefix + "Send\t" + getId() + "\tStart:" + getTime_start() + "\tEnd:" + getTime_end() + "\t" + isApproved() + "\tAmount:" + getAmount() + "\tLine_id:" + line_ids[0] + "\tBandwidth:" + NetSimulation.getTopology().lines.get(line_ids[0]).getBandwidth() + "\n");
        }catch( Exception ex )
        {
            System.out.println(ex.getMessage());
        }
    }

    public double getShort_path_time_end()
    {
        return getTime_end();
    }

    protected void setTime_end(double time_end)
    {
        super.setTime_end( time_end );
        setShort_path_time_end( time_end );
    }
}
